// import { Pen } from '@meta2d/core';
// import { reactive } from 'vue';

// export enum SelectionMode {
//   File,
//   Pen,
// }

// const selections = reactive<{
//   mode: SelectionMode;
//   pen?: Pen[];
// }>({
//   // 选中对象类型：0 - 画布；1 - 单个图元
//   mode: SelectionMode.File,
//   pen: undefined,
// });

// export const useSelection = () => {
//   const select = (pens?: Pen[]) => {
//     if (!pens || pens.length !== 1) {
//       selections.mode = SelectionMode.File;
//       selections.pen = undefined;
//       return;
//     }

//     selections.mode = SelectionMode.Pen;
//     selections.pen = pens[0]; //丢失对原始数组的响应性引用
//     // if (selections.pen) {  
//     //   selections.pen.splice(0, selections.pen.length); // 清空数组  
//     //   selections.pen.push(...pens); // 添加新元素  
//     // } else {  
//     //   selections.pen = [...pens]; // 如果之前未定义，则直接创建响应式数组  
//     // }
//     // console.log('selections.pen', selections.pen)
//   };
//   return {
//     selections,
//     select,
//   };
// };

import { Pen } from '@meta2d/core';
import { reactive } from 'vue';

export enum SelectionMode {
  File,
  Pen,
}

const selections = reactive<{
  mode: SelectionMode;
  pen?: Pen;
}>({
  // 选中对象类型：0 - 画布；1 - 单个图元
  mode: SelectionMode.File,
  pen: undefined,
});

export const useSelection = () => {
  const select = (pens?: Pen[]) => {
    if (!pens || pens.length !== 1) {
      selections.mode = SelectionMode.File;
      selections.pen = undefined;
      return;
    }

    selections.mode = SelectionMode.Pen;
    selections.pen = pens[0];
  };
  return {
    selections,
    select,
  };
};